import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { ObjCreate } from "../../common/ObjCreate";
import { Language } from "../../../../../extend/language/Language";
import { JumpFloorItem } from "./com/JumpFloorItem";
import { GlobalSkin } from "../../../../component/skin/GlobalSkin";
import { JumpFloorItemInfo } from "./com/JumpFloorItemInfo";
import { CopyUtil } from "../CopyUtil";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { MagicTowerConfig } from "../../../resource/MagicTowerConfig";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { Cache } from "../../../cache/Cache";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { NpcEffectRule } from "../../../rules/NpcEffectRule";
import { ScaleBitmap } from "../../../../../com/mui/display/ScaleBitmap";
import { GLabel } from "../../../../../com/mui/controls/GLabel";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
import { GButton } from "../../../../../com/mui/controls/GButton";
type int = number;
//class JumpFloorWindow
    
    export  class JumpFloorWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            setSize(320, 350);
            titleHeight = 25;
            title = ObjCreate.createTitleTextField(Language.getString(60394));
            this.initUI();
            return;
        }

        protected initUI(): void
        {
            this._bg = ObjCreate.createBg(18, 37, 284, 300, this);
            this._floorList = ObjCreate.createTileList(25, 50, 270, 256, this);
            this._floorList.setStyle("cellRenderer", JumpFloorItem);
            this._floorList.columnWidth = 260;
            this._floorList.rowHeight = 30;
            this._okBtn = ObjCreate.createGButton(Language.getString(69901), 40, 300, 70, 26, this, GlobalSkin.ButtonRed);
            this._enterBtn = ObjCreate.createGButton(Language.getString(41847), 125, 300, 70, 26, this, GlobalSkin.ButtonRed);
            this._cancelBtn = ObjCreate.createGButton(Language.getString(69902), 210, 300, 70, 26, this, GlobalSkin.ButtonRed);
            this._okBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this._enterBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this._cancelBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=arg1.target;
            switch (loc2) 
            {
                case this._okBtn:
                {
                    loc1 = this._floorList.selectedItem as JumpFloorItemInfo;
                    if (loc1 && loc1.selectable) 
                    {
                        CopyUtil.toWhichFloor = loc1.floor;
                    }
                    else 
                    {
                        CopyUtil.toWhichFloor = 0;
                    }
                    this.hide();
                    break;
                }
                case this._enterBtn:
                {
                    if (this._copy) 
                    {
                        loc1 = this._floorList.selectedItem as JumpFloorItemInfo;
                        if (loc1 && loc1.selectable) 
                        {
                            CopyUtil.toWhichFloor = loc1.floor;
                        }
                        else 
                        {
                            CopyUtil.toWhichFloor = 0;
                        }
                        this.hide();
                        Dispatcher.dispatchEvent(new DataEvent(EventName.CopyCopyChooseFromNpc, this._copy.code));
                    }
                    break;
                }
                case this._cancelBtn:
                {
                    CopyUtil.toWhichFloor = 0;
                    this.hide();
                    break;
                }
            }
            return;
        }

        public setCopyInfo(arg1: TCopy): void
        {
            this._copy = arg1;
            this.updateDataProvider();
            return;
        }

        public updateDataProvider(): void
        {
            var loc1=null;
            var loc2=null;
            if (this._copy) 
            {
                loc1 = MagicTowerConfig.instance.getJumpFloorList(this._copy.code);
                if (loc1) 
                {
                    loc2 = this.updateDpStatus(loc1);
                    this._floorList.dataProvider = loc1;
                    this._floorList.selectedItem = loc2;
                    this._floorList.sortItemsOn("floor", Array.NUMERIC);
                }
                else 
                {
                    this._floorList.dataProvider = new DataProvider();
                }
            }
            else 
            {
                this._floorList.dataProvider = new DataProvider();
            }
            this.updatePosition();
            return;
        }

        /* internal  */updateDpStatus(arg1: DataProvider): JumpFloorItemInfo
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=0;
            var loc6=null;
            var loc1=Cache.instance.role.roleInfo.level;
            loc5 = 0;
            while (loc5 < arg1.length) 
            {
                loc3 = arg1.getItemAt(loc5) as JumpFloorItemInfo;
                loc2 = Cache.instance.copy.getCopyNumInfoByCode(loc3.info.copyCode);
                if (loc2) 
                {
                    if (loc3.info.floor < loc2.maxProcess && loc3.info.jumpLevel <= loc1) 
                    {
                        loc3.selectable = true;
                        loc3.text = Language.getStringByParam(60393, loc3.floor) + "（" + loc3.info.jumpCost + GameDefConfig.instance.getEPrictUnitName(EPrictUnit._EPriceUnitGold) + "）";
                    }
                    else if (loc3.info.floor == loc2.maxProcess && loc3.info.jumpLevel <= loc1) 
                    {
                        if (loc3.info.jumpTime > loc2.maxProcessTime) 
                        {
                            loc3.selectable = false;
                            loc3.text = Language.getStringByParam(60396, loc3.floor, loc3.info.jumpTime) + "（" + loc2.maxProcessTime + "/" + loc3.info.jumpTime + "）";
                        }
                        else 
                        {
                            loc3.selectable = true;
                            loc3.text = Language.getStringByParam(60393, loc3.floor) + "（" + loc3.info.jumpCost + GameDefConfig.instance.getEPrictUnitName(EPrictUnit._EPriceUnitGold) + "）";
                        }
                    }
                    else 
                    {
                        loc3.selectable = false;
                        loc3.text = "";
                        if (loc3.info.jumpLevel > loc1) 
                        {
                            loc3.text = loc3.text + (Language.getStringByParam(60395, loc3.info.jumpLevel) + "，");
                        }
                        loc3.text = loc3.text + Language.getStringByParam(60396, loc3.floor, loc3.info.jumpTime);
                        if (loc3.info.floor <= loc2.maxProcess) 
                        {
                            loc3.text = loc3.text + ("（" + loc2.maxProcessTime + "/" + loc3.info.jumpTime + "）");
                        }
                    }
                }
                else 
                {
                    loc3.selectable = false;
                    loc3.text = "";
                }
                if (loc3.selectable && loc3.floor > loc4) 
                {
                    loc4 = loc3.floor;
                }
                ++loc5;
            }
            loc5 = 0;
            while (loc5 < arg1.length) 
            {
                if ((loc3 = arg1.getItemAt(loc5) as JumpFloorItemInfo).floor != loc4) 
                {
                    loc3.selected = false;
                }
                else 
                {
                    loc3.selected = true;
                    loc6 = loc3;
                }
                ++loc5;
            }
            return loc6;
        }

        /* internal  */updatePosition(): void
        {
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            NpcEffectRule.registDialogEffectWindow(this);
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.NpcUpdateDialogInfo));
            return;
        }

        /* internal */ /* var */_bg: ScaleBitmap;

        /* internal */ /* var */_note: GLabel;

        /* internal */ /* var */_copy: TCopy;

        /* internal */ /* var */_floorList: GTileList;

        /* internal */ /* var */_okBtn: GButton;

        /* internal */ /* var */_enterBtn: GButton;

        /* internal */ /* var */_cancelBtn: GButton;
    }
